<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class omnitrix_html_wrapper extends omnitrix_html_container
{
	protected $_form_action;
	protected $_form_method;
	protected $_form_buttons = array();
	protected $_form_errors = array();

	public function __construct($form_action = null, $form_method = null, $submit_button = true)
	{
		if (null == $form_action) {
			$form_action = otx_request_uri();
		}

		if (null == $form_method) {
			$form_method = 'post';
		}

		$this->set_form_action($form_action)
			->set_form_method($form_method);

		if (true === $submit_button) {
			$this->add_submit();
		} else if (is_string($submit_button)) {
			$this->add_submit($submit_button);
		}
	}

	public function set_form_action($form_action)
	{
		$this->_form_action = $form_action;
		return $this;
	}

	public function set_form_method($form_method)
	{
		$this->_form_method = $form_method;
		return $this;
	}

	public function set_form_errors($form_errors)
	{
		$this->_form_errors = $form_errors;
		return $this;
	}

	public function create_table($table_title = null, $table_class = null)
	{
		$table = otx_html_tag_custom('table', $table_title, $table_class);
		$this->add_child($table);
		return $table;
	}

	public function add_submit($text = null)
	{
		if (null === $text) {
			$text = otx_language_parse('button_save');
		}
		$this->_form_buttons[] = otx_html_form_button(null, $text, array(
				'class' => 'submit-button',
				'type' => 'submit'
		));
		return $this;
	}

	public function add_button($button)
	{
		$this->_form_buttons[] = $button;
		return $this;
	}

	public function render_html()
	{
		return otx_html_tag_start('form', array(
				'action' => $this->_form_action,
				'method' => $this->_form_method))
				. $this->_render_errors()
				. parent::render_html()
				. $this->_render_buttons()
				. otx_html_tag_end('form');
	}

	protected function _render_errors()
	{
		if (empty($this->_form_errors)) {
			return '';
		}

		$html = otx_html_tag_start('div', array('class' => 'warning'))
				. otx_html_tag_simple('span', otx_language_parse('text_errors'))
				. otx_html_tag_start('ul');

		for ($i = 0; $i < count($this->_form_errors); $i++) {
			$html .= otx_html_tag_simple('li', $this->_form_errors[$i]);
		}

		$html .= otx_html_tag_end('ul')
				. otx_html_tag_end('div');

		return $html;
	}

	protected function _render_buttons()
	{
		if (empty($this->_form_buttons)) {
			return '';
		}

		$html = otx_html_tag_start('div', array('class' => 'button-wrapper'));

		for ($i = 0; $i < count($this->_form_buttons); $i++) {
			$html .= $this->_form_buttons[$i];
		}

		$html .= otx_html_tag_end('div');

		return $html;
	}
}